package com.atbun.statisticsservice.service.impl;

import com.atbun.commonutils.R;
import com.atbun.statisticsservice.client.UcenterClient;
import com.atbun.statisticsservice.entity.StatisticsDaily;
import com.atbun.statisticsservice.mapper.StatisticsDailyMapper;
import com.atbun.statisticsservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-05-22
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Autowired
    private UcenterClient ucenterClient;

//    统计每天注册人数，把数据添加到statistics_daily(统计表)表中
    @Override
    public void createStatisticsByDay(String day) {
//        添加统计人数时，先删除同一天表中的数据。
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date_calculated",day);
        baseMapper.delete(queryWrapper);

//        查询用户每天注册的人数，取出map对象中的值，因为返回的值就是封装了map
        Integer registerNum = (Integer) ucenterClient.countRegister(day).getData().get("countRegister");
//        使用随机数模拟数据
        Integer loginNum = RandomUtils.nextInt(100, 200);//TODO
        Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO
        Integer courseNum = RandomUtils.nextInt(100, 200);//TODO

        //创建统计对象
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(registerNum);
        daily.setLoginNum(loginNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setCourseNum(courseNum);
        daily.setDateCalculated(day);

        baseMapper.insert(daily);
    }

    /**
     * 根据值生成统计图
     * @param begin
     * @param end
     * @param type
     * @return
     */
    @Override
    public Map<String, Object> showChart(String begin, String end, String type) {
//        根据统计时间查询统计表
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
//          查找指定字段
        queryWrapper.select(type,"date_calculated");
//        根据时间区间查找
        queryWrapper.between("date_calculated",begin,end);
        List<StatisticsDaily> dayList = baseMapper.selectList(queryWrapper);

        /**
         * 因为返回有两部分数据，日期和日期对应的数量
         * 前端是json，后端是list集合
         * 创建两个list集合，一个是日期list，一个是数量list
         */
        List<String> dateList = new ArrayList<>();
        List<Integer> numDateList = new ArrayList<>();

        Map<String,Object> map = new HashMap<>();
        map.put("dateList",dateList);
        map.put("numDateList",numDateList);

        for (int i=0;i<dayList.size();i++){
            StatisticsDaily daily = dayList.get(i);
//            封装日期的集合
            dateList.add(daily.getDateCalculated());
            switch (type){
//                case中的值，是前端传过来的值
                case "register_num":
                    numDateList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    numDateList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    numDateList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numDateList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }

        }
        return map;
    }
}
